package stat;

public class MPDerivedStat extends DerivedStat {
    
    private int cachedMax;
    
    /**
     * take in a variable number of stats to observe
     * @param st
     * @param toCache
     */
    public MPDerivedStat( StatType st, Stat... toCache) {
        super(st, toCache);
        if(toCache.length > 0) {
            setValue(getFromCache(StatType.MAXMP));
            cachedMax = getFromCache(StatType.MAXMP);
        }
        //compute();
    }
    
    public MPDerivedStat( StatType st, int value, Stat... toCache ) {
        super(st, value, toCache);
        if(toCache.length > 0) {
            setValue(getFromCache(StatType.MAXMP));
            cachedMax = getFromCache(StatType.MAXMP);
        }
        //compute();
    }
    
    /**
     * compuet new mp based on maxmp
     */
    public void compute() {
        if(getFromCache(StatType.MAXMP) > cachedMax) {
            setValue(getValue() + Math.abs(getFromCache(StatType.MAXMP) - cachedMax));
            cachedMax = getFromCache(StatType.MAXMP);
        }
    }
    
    /**
     * set new value if >= 0 and < maxmp
     */
    public void setValue(int val) {
        if(shouldDerive() && getFromCache(StatType.MAXMP) < val ) {
            val = getFromCache(StatType.MAXMP);
        }
        
        super.setValue(val);
    }

}
